package com.kc.frontEnd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kc.saf.domain.*;
import com.kc.saf.service.*;
import com.kc.sys.config.LoginIntercepetor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author forever_love
 * @create 2019--11--11 11:36
 **/
@RequestMapping("yanAnDepot")
@Controller
public class frontEndController {

    @Autowired
    private CaseManagementService caseManagementService;
    @Autowired
    private CoreManagementService coreManagementService;
    @Autowired
    private CaseClassicService caseClassicService;
    @Autowired
    private SafetyAlertTypeService safetyAlertTypeService;
    @Autowired
    private CoreTypeService CoreTypeService;

    @RequestMapping
    public String index(HttpServletRequest request, HttpServletResponse response, Object handler){
        LoginIntercepetor loginIntercepetor = new LoginIntercepetor();
        loginIntercepetor.preHandle(request,response,handler);

        return "/frontEnd/index";
    }


    @RequestMapping("details/{id}")
    public String details(@PathVariable("id") Integer id, Model model){
        CaseManagement caseManagement = caseManagementService.getById(id);
        model.addAttribute("caseManagement",caseManagement);
        return "/frontEnd/details";
    }

    @RequestMapping("fourdetails/{id}")
    public String fourdetails(@PathVariable("id") Integer id, Model model){
        CoreManagement coreManagement = coreManagementService.getById(id);
        model.addAttribute("coreManagement",coreManagement);
        return "/frontEnd/fourDetails";
    }
    @RequestMapping("caseClassic/{id}")
    public String caseClassic(@PathVariable("id") Integer id, Model model){
        CaseClassic caseClassic = caseClassicService.getById(id);
        model.addAttribute("caseClassic",caseClassic);
        return "/frontEnd/img";
    }

    @RequestMapping("caseClassicPPT/{coreId}")
    public String caseClassicPPT(@PathVariable("coreId") Integer coreId, Model model){
        QueryWrapper<CaseClassic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(coreId!=null,"core_id",coreId);
        queryWrapper.eq("available",1);
        CoreType coreType = CoreTypeService.getById(coreId);
        QueryWrapper<SafetyAlertType> coreTypeWrapper = new QueryWrapper<>();
        coreTypeWrapper.eq(coreType.getSafetyId()!=null,"id",coreType.getSafetyId());
        List<SafetyAlertType> safetyAlertTypes = safetyAlertTypeService.list(coreTypeWrapper);
        if(safetyAlertTypes.size()>0){
            model.addAttribute("safetyTypeName",safetyAlertTypes.get(0).getTypename());
        }
        List<CaseClassic> caseClassics = caseClassicService.list(queryWrapper);
        model.addAttribute("caseClassics",caseClassics);
        return "/frontEnd/PPTdetails";
    }

    @RequestMapping("caseClassicVideo/{coreId}")
    public String caseClassicVideo(@PathVariable("coreId") Integer coreId, Model model){
        QueryWrapper<CaseClassic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(coreId!=null,"core_id",coreId);
        queryWrapper.eq("available",1);
        CoreType coreType = CoreTypeService.getById(coreId);
        QueryWrapper<SafetyAlertType> coreTypeWrapper = new QueryWrapper<>();
        coreTypeWrapper.eq(coreType.getSafetyId()!=null,"id",coreType.getSafetyId());
        List<SafetyAlertType> safetyAlertTypes = safetyAlertTypeService.list(coreTypeWrapper);
        if(safetyAlertTypes.size()>0){
            model.addAttribute("safetyTypeName",safetyAlertTypes.get(0).getTypename());
        }
        List<CaseClassic> caseClassics = caseClassicService.list(queryWrapper);
        model.addAttribute("caseClassics",caseClassics);
        return "/frontEnd/VideoDetails";
    }

    @RequestMapping("loadCaseManagement/{safetyId}")
    public String loadCaseManagement(@PathVariable("safetyId") Integer safetyId, Model model){
        QueryWrapper<CaseManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(safetyId!=null,"safety_id",safetyId);
        queryWrapper.eq("available",1);
        //queryWrapper.orderByDesc("createDate");
        QueryWrapper<SafetyAlertType> coreTypeWrapper = new QueryWrapper<>();
        SafetyAlertType safetyAlertType = safetyAlertTypeService.getById(safetyId);
        coreTypeWrapper.eq(safetyAlertType.getId()!=null,"id",safetyAlertType.getId());
        List<SafetyAlertType> safetyAlertTypes = safetyAlertTypeService.list(coreTypeWrapper);
        if(safetyAlertTypes.size()>0){
            model.addAttribute("safetyTypeName",safetyAlertTypes.get(0).getTypename());
        }
        List<CaseManagement> caseManagements = caseManagementService.list(queryWrapper);
        model.addAttribute("caseManagements",caseManagements);
        return "/frontEnd/caseList";
    }
}
